package cn.zhonghuanzhiyuan.sprc.basicinfo.service;

import cn.hutool.core.util.IdUtil;
import cn.org.bachelor.iam.IamContext;
import cn.zhonghuanzhiyuan.sprc.basicinfo.dao.DemoMapper;
import cn.zhonghuanzhiyuan.sprc.basicinfo.domain.Demo;
import cn.zhonghuanzhiyuan.sprc.basicinfo.pojo.vo.AddDemoVo;
import cn.zhonghuanzhiyuan.sprc.basicinfo.pojo.vo.QueryDemoVo;
import cn.zhonghuanzhiyuan.sprc.basicinfo.pojo.vo.UpdateDemoVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: eprs
 * @BelongsPackage: cn.zhonghuanzhiyuan.eprs.command.service
 * @Author: Hyw
 * @CreateTime: 2024-01-08  14:43
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
@Service
public class DemoService {

    @Autowired
    private DemoMapper demoMapper;

    @Autowired
    private IamContext iamContext;

    public void saveDemo(AddDemoVo vo) {
        log.info("saveDemo start");
        log.info("新增Demo参数：{}", vo);
        String userName = iamContext.getUser().getName();
        Date addDate = new Date();
        Demo demo = new Demo();
        saveOrUpdate(demo, vo);
        demo.setId(IdUtil.simpleUUID());
        demo.setCreateTime(addDate);
        demo.setCreateUser(userName);
        demo.setUpdateTime(addDate);
        demo.setUpdateUser(userName);
        //demoMapper.insertSelective() // demoMapper.insertSelective()方法用于将一个对象插入到数据库表中，只插入非空属性对应的列。
        demoMapper.insert(demo);
        log.info("saveDemo end");
    }

    private void saveOrUpdate(Demo demo, AddDemoVo vo) {
        demo.setVehicleNum(vo.getVehicleNum());
        demo.setVehicleType(vo.getVehicleType());
        demo.setWorkUnit(vo.getWorkUnit());
        demo.setUnitAddress(vo.getUnitAddress());
        demo.setContactPersonName(vo.getContactPersonName());
        demo.setContactPersonTel(vo.getContactPersonTel());
        demo.setEnclosureImage(vo.getEnclosureImage());
        demo.setEnclosureFile(vo.getEnclosureFile());
    }

    public void deleteDemoByIds(String ids) {
        log.info("deleteDemoByIds start");
        log.info("删除主键集合或者单个：{}", ids);
        System.out.println(Arrays.asList(ids.split(",")));
        if (StringUtils.isNotEmpty(ids)) {
            Arrays.asList(ids.split(",")).stream().forEach(id -> {
                Example example = new Example(Demo.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("id", id);
                demoMapper.delete(demoMapper.selectOneByExample(example));
            });
        }
        log.info("deleteDemoByIds end");
    }

    public void updateDemo(UpdateDemoVo vo) {
        log.info("updateDemo start");
        log.info("修改Demo参数：{}", vo);
        String updateUserName = iamContext.getUser().getName();
        Date updateDate = new Date();
        Example example = new Example(Demo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", vo.getId());
        Demo demo = demoMapper.selectOneByExample(example);
        saveOrUpdate(demo, vo);
        demo.setUpdateTime(updateDate);
        demo.setUpdateUser(updateUserName);
        demoMapper.updateByExampleSelective(demo, example);
        log.info("updateDemo end");
    }

    public List<Demo> selectDemoPage(QueryDemoVo demoQueryVo) {
        log.info("selectDemoPage start");
        log.info("demo查询参数：{}", demoQueryVo);
        Example example = new Example(Demo.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(demoQueryVo.getVehicleNum()))
            criteria.andEqualTo("vehicleNum", demoQueryVo.getVehicleNum());
            //criteria.andLike("驼峰字段", "%" + demoQueryVo.getVehicleNum() + "%");
        log.info("selectDemoPage end");
        return demoMapper.selectByExample(example);
    }

    public Demo getDemoById(String id) {
        log.info("getDemoById start");
        log.info("获取demo详情主键：{}", id);
        Example example = new Example(Demo.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(id))
            criteria.andEqualTo("id", id);
        log.info("getDemoById end");
        return demoMapper.selectOneByExample(example);
    }
}
